//
//  DayCell.m
//  MyTrip
//
//  Created by zhang on 15/8/25.
//  Copyright (c) 2015年 张益. All rights reserved.
//

#import "DayCell.h"
#import "UIImageView+WebCache.h"
#define Width [[UIScreen mainScreen] bounds].size.width

@implementation DayCell

- (void)dealloc
{
    [_diaryTextLabel release];
    [_timeLabel release];
    [_picView release];
    [super dealloc];
}

- (instancetype) initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        self.picView = [[[UIImageView alloc] init] autorelease];
        [self.contentView addSubview:_picView];
        
        self.diaryTextLabel = [[[UILabel alloc] init] autorelease];
        [_diaryTextLabel setFont:[UIFont systemFontOfSize:11]];
        [_diaryTextLabel setTextAlignment:NSTextAlignmentLeft];
        _diaryTextLabel.numberOfLines = 0;
        [self.contentView addSubview:_diaryTextLabel];
        
        self.timeView = [[[UIImageView alloc] init] autorelease];
        [self.contentView addSubview:_timeView];
        
        
        self.timeLabel = [[[UILabel alloc] init] autorelease];
        [_timeLabel setFont:[UIFont systemFontOfSize:13]];
        _timeLabel.alpha = 0.5;
        [self.contentView addSubview:_timeLabel];
        
    }
    return self;
}




+ (CGFloat)hightWithIndexPathOfDic:(NSDictionary *)localDic{
    NSDictionary *photoInfo = [localDic objectForKey:@"photo_info"];
    NSNull *nu = [NSNull null];
    NSInteger width = 0;
    NSInteger height = 0;
    if (![photoInfo isEqual:nu]) {
        width = [[photoInfo objectForKey:@"w"] integerValue];
        height = [[photoInfo objectForKey:@"h"] integerValue];
    }

    NSString *text = [localDic objectForKey:@"text"];
    NSString *photo = [localDic objectForKey:@"photo_1600"];
    CGFloat photoHeight = (Width - 10 * OffWidth) * height / width;
    if ([text  isEqual: @""] && ![photo isEqualToString:@""]) {
        
//        NSLog(@"photo ==== ");
        return 25 * OffHeight + photoHeight;
    }
    if ([photo  isEqual: @""] && ![text isEqualToString:@""])  {
        NSString *text = [localDic objectForKey:@"text"];
        NSDictionary *dic = @{NSFontAttributeName:[UIFont systemFontOfSize:13]};
        CGRect rect = [text boundingRectWithSize:CGSizeMake(Width - 15 * OffWidth - 14 * OffWidth, 1000000000) options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil];
//        NSLog(@"text ==== %f", rect.size.height);
        return rect.size.height + 25 * OffHeight;
    }
    if (![text isEqualToString:@""] && ![photo isEqualToString:@""]){
        NSString *text = [localDic objectForKey:@"text"];
        NSDictionary *dic = @{NSFontAttributeName:[UIFont systemFontOfSize:13]};
        CGRect rect = [text boundingRectWithSize:CGSizeMake((Width - 15 - 14) * OffWidth, 1000000000) options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil];
//        NSLog(@"text + photo ===  %f", rect.size.height);
        return rect.size.height + 25  * OffHeight + photoHeight + 10 * OffHeight;
    }
    return 0;
}



- (void)createViewWithDic:(NSDictionary *)diaryDic{
    NSString *text = [diaryDic objectForKey:@"text"];
    NSString *photo = [diaryDic objectForKey:@"photo_1600"];
    NSString *time = [diaryDic objectForKey:@"local_time"];
    NSDictionary *photoInfo = [diaryDic objectForKey:@"photo_info"];
    NSNull *nu = [NSNull null];
    NSInteger width = 0;
    NSInteger height = 0;
    if (![photoInfo isEqual:nu]) {
        width = [[photoInfo objectForKey:@"w"] integerValue];
        height = [[photoInfo objectForKey:@"h"] integerValue];
    }
    
//    NSRange range = [time rangeOfString:@":00"];
//    NSLog(@"%ld, %ld", range.length, range.location);
//    NSRange aRange = NSMakeRange(16, 3);
    time = [time stringByReplacingOccurrencesOfString:@"-" withString:@"."];
    
    if ([text  isEqual: @""] && ![photo isEqualToString:@""]) {
        CGFloat photoHeight = (Width - 10 * OffWidth) * height / width;
        
        [_picView setFrame:CGRectMake(5 * OffWidth, 5 * OffHeight, Width - 10 * OffWidth, photoHeight)];
        [_picView sd_setImageWithURL:[NSURL URLWithString:photo] placeholderImage:[UIImage imageNamed:@"zhanweitu.jpg"]];
        [_timeView setFrame:CGRectMake(7 * OffWidth, _picView.frame.size.height + _picView.frame.origin.y, 20 * OffWidth, 20 * OffHeight)];
        [_timeLabel setFrame:CGRectMake(_timeView.frame.origin.x + _timeView.frame.size.width, _picView.frame.size.height + _picView.frame.origin.y, 200 * OffWidth, 20 * OffHeight)];
        [_timeView setImage:[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"time" ofType:@"png"]]];
        [_timeLabel setText:time];
        [_diaryTextLabel setFrame:CGRectZero];
        return;
    } else {
        [_picView setFrame:CGRectZero];
    }
    
    if ([photo  isEqual: @""] && ![text isEqualToString:@""]) {
        NSDictionary *dic = @{NSFontAttributeName:[UIFont systemFontOfSize:13]};
        CGRect rect = [text boundingRectWithSize:CGSizeMake(Width - 15 * OffWidth - 14 * OffWidth, 1000000000) options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil];
        [self.diaryTextLabel setFrame:CGRectMake(7 * OffWidth, 7 * OffHeight, rect.size.width, rect.size.height * OffHeight)];
        [self.diaryTextLabel setText:text];
        [_timeView setFrame:CGRectMake(7 * OffWidth, _diaryTextLabel.frame.size.height + _diaryTextLabel.frame.origin.y, 20 * OffWidth, 20 * OffHeight)];
       [_timeLabel setFrame:CGRectMake(_timeView.frame.origin.x + _timeView.frame.size.width, _diaryTextLabel.frame.size.height + _diaryTextLabel.frame.origin.y, 200 * OffWidth, 20 * OffHeight)];
        [_timeView setImage:[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"time" ofType:@"png"]]];
        [_timeLabel setText:time];
         [_picView setFrame:CGRectZero];
        
        return;
    
    } else {
        [self.diaryTextLabel setFrame:CGRectZero];
    }
    
    if (![text isEqualToString:@""] && ![photo isEqualToString:@""]) {
        
        CGFloat photoHeight = (Width - 10 * OffWidth) * height / width;
        
        [_picView setFrame:CGRectMake(5 * OffWidth, 5 * OffHeight, Width - 10 * OffWidth, photoHeight)];
        [_picView sd_setImageWithURL:[NSURL URLWithString:photo] placeholderImage:[UIImage imageNamed:@"zhanweitu.jpg"]];
        
        NSDictionary *dic = @{NSFontAttributeName:[UIFont systemFontOfSize:13]};
        CGRect rect = [text boundingRectWithSize:CGSizeMake(Width - 15 * OffWidth - 14 * OffWidth, 1000000) options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil];

        [self.diaryTextLabel setFrame:CGRectMake(7 * OffWidth, 7 * OffHeight + photoHeight, rect.size.width , rect.size.height + 10 * OffHeight)];
        [self.diaryTextLabel setText:text];
        [_timeView setFrame:CGRectMake(7 * OffWidth, _diaryTextLabel.frame.size.height + _diaryTextLabel.frame.origin.y , 20 * OffWidth, 20 * OffHeight)];
        [_timeLabel setFrame:CGRectMake(_timeView.frame.origin.x + _timeView.frame.size.width, _diaryTextLabel.frame.size.height + _diaryTextLabel.frame.origin.y , 200 * OffWidth, 20 * OffHeight)];
        [_timeView setImage:[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"time" ofType:@"png"]]];
        [_timeLabel setText:time];
        
        
        return;
    
    } else {
        [self.diaryTextLabel setFrame:CGRectZero];
         [_picView setFrame:CGRectZero];
    }
    
}







@end
